﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PersonWizardPageView.xaml.cs" company="WildGums">
//   Copyright (c) 2008 - 2015 WildGums. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------


namespace Orc.Wizard.Example.Wizard.Views
{
    using System;
    using System.IO;
    using System.Windows;
    using System.Windows.Navigation;

    public partial class PersonWizardPageView
    {
        public PersonWizardPageView()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (cheackpath())
            {
                MessageBox.Show(string.Format("'{0}'、'{1}'或'{2}'目录已存在,请先删除目录！！！", @"D:\SMOP", @"D:\wwwroot", @"D:\Services"));
            }
            else
            {
                ProgressBar.Maximum = 90;
                ProgressBar.Value = 0;
                string syspath = System.IO.Directory.GetCurrentDirectory();
                string destFile = @"D:\";
                string[] path = { @"..\Services", @"..\wwwroot", @"..\SMOP" };
                string basepath = System.AppDomain.CurrentDomain.BaseDirectory;
              

                foreach (var sourceFile in path)
                {
                    var source = Path.Combine(basepath, Path.GetFileName(sourceFile));
                    if (!Directory.Exists(sourceFile))
                    {
                        MessageBox.Show(string.Format("{0}不存在", sourceFile));
                    }
                    else
                    {
                        ProgressBar.Value += 30;
                        CopyDirectory(sourceFile, destFile);
                    }
                }               
            } 
                                                                  
        }

        public bool cheackpath()
        {
            bool flag;
            flag = false;
            if (Directory.Exists(@"D:\SMOP"))
            {
                if (Directory.Exists(@"D:\wwwroot"))
                {
                    if (Directory.Exists(@"D:\Services"))
                    {
                        flag = true;
                    }
                }

            }

            return flag;
        }

        public bool CopyDirectory(string sourceFile, string destFile)
        {
            bool flag = false;
            if (File.Exists(sourceFile))
            {

                try
                {
                    if (!Directory.Exists(destFile))
                    {
                        Directory.CreateDirectory(destFile);
                    }
                    File.Copy(sourceFile, Path.Combine(destFile, Path.GetFileName(sourceFile)));
                    flag = true;
                }
                catch (System.Exception ex)
                {
                }            
            }
            if (Directory.Exists(sourceFile))
            {
                //将文件的只读属性去掉
                string[] fileList = Directory.GetFileSystemEntries(sourceFile);
                string lastpayh = Path.Combine(destFile, Path.GetFileName(sourceFile));
                if (!Directory.Exists(lastpayh))
                {
                    Directory.CreateDirectory(lastpayh);
                    System.IO.DirectoryInfo DirInfo = new DirectoryInfo(lastpayh);
                    DirInfo.Attributes = FileAttributes.Normal & FileAttributes.Directory;
                }
                if (destFile[destFile.Length - 1] != System.IO.Path.DirectorySeparatorChar)
                {
                    destFile += System.IO.Path.DirectorySeparatorChar;
                }
                foreach (string file in fileList)
                {
                    // 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
                    if (Directory.Exists(file))
                    {
                        CopyDirectory(file, Path.Combine(destFile, Path.GetFileName(sourceFile)));
                    }
                    // 否则直接Copy文件
                    else
                    {
                       
                        System.IO.File.Copy(file, Path.Combine(lastpayh, Path.GetFileName(file)), true);                       
                        System.IO.File.SetAttributes(Path.Combine(lastpayh, Path.GetFileName(file)), System.IO.FileAttributes.Normal);
                    }
                }
                flag = true;
            }
            return flag;
        }       
    }
}